//
//  MainViewController.m
//  iRPN
//
//  Created by Nathan Peterson on 5/2/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "MainViewController.h"
#import "MainView.h"
#import "grpn.h"

@implementation MainViewController

@synthesize tableView;
@synthesize editingField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
        GRPN_init();
    }
    return self;
}

- (IBAction)EnterButtonPress:(id)sender {
    if([editingField.text length] > 0){
    readLine([editingField.text cStringUsingEncoding:NSASCIIStringEncoding]);
    editingField.text = @"";
    [tableView reloadData];
    }else{
        DupStack();
        [tableView reloadData];
    }
}

- (IBAction)SwapButtonPress:(id)sender {
    if([editingField.text length] > 0){
        [self EnterButtonPress:sender];
    }
    SwapStack();
    [tableView reloadData];
}

- (IBAction)DropButtonPress:(id)sender {
    PopStack();
    [tableView reloadData];
}

- (IBAction)DeleteButtonPress:(id)sender {
    if([editingField.text length] > 0){
        NSInteger editingLength = [editingField.text length] - 1;
        editingField.text = [editingField.text substringToIndex:editingLength];
    }else{
        [self DropButtonPress:sender];
    }
}

- (IBAction)OperatorButtonPress:(id)sender {
    [self EnterButtonPress:sender];
    UIButton* button = sender;
    //NSLog(@"Sender is:%d\n", button.tag);
    switch (button.tag) {
        case 100:
            NegStack();
            break;
        case 101:
            SqrtStack();
            break;
        case 102:
            InvStack();
            break;
        case 103:
            NrootStack();
            break;
        case 104:
            PiStack();
            break;
        case 105:
            DivStack();
            break;
        case 106:
            MulStack();
            break;
        case 107:
            AddStack();
            break;
        case 108:
            SubStack();
            break;
        case 109:
            SqrStack();
            break;
        case 110:
            PowStack();
            break;
        case 111:
            TenxStack();
            break;
        case 112:
            ExpStack();
            break;
        default:
            break;
    }
    [tableView reloadData];
}

- (IBAction)NumberKeyPress:(id)sender {
    UIButton* button = sender;
    editingField.text = [editingField.text stringByAppendingString:button.currentTitle];
	//NSLog(@"i got pressed by %@", button.currentTitle);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return LCD_HEIGHT;
    //return stackLen();
}

- (UITableViewCell *)tableView:(UITableView *)thetableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identity = @"MainCell";
    UITableViewCell *cell = [thetableView dequeueReusableCellWithIdentifier:identity];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identity] autorelease];
    }
    
    int elementNumber = LCD_HEIGHT-1-indexPath.row;
    //int elementNumber = stackLen()-1-indexPath.row;
    if(elementNumber < stackLen())
        [cell setText:[[NSString alloc] initWithCString:printNumber(getStackEle(LCD_HEIGHT-1-indexPath.row)) encoding:NSASCIIStringEncoding]];
    else
        [cell setText:@""];
    
    return cell;
}

- (BOOL)tableView:(UITableView *)thetableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [tableView reloadData];
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:LCD_HEIGHT-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:false];
    [super viewDidLoad];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}


@end
